package org.ruiyichen.guizhou.agr.ui.mineview.html5;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.ProgressBar;


/**
 * 作者：Administrator
 * 邮箱：wutian8489@foxmail.com
 */
public class H5GeoClient extends WebChromeClient {

    private Activity mContext;

    private ProgressBar progressbar;

    private H5GeoClient() {

    }

    public H5GeoClient(Activity context) {
        this.mContext = context;
    }

    @Override
    public void onGeolocationPermissionsShowPrompt(String origin,
                                                   android.webkit.GeolocationPermissions.Callback callback) {
        super.onGeolocationPermissionsShowPrompt(origin, callback);
        callback.invoke(origin, true, false);
    }

    @Override
    public boolean onJsAlert(WebView view, String url,
                             String message, final JsResult result) {

        AlertDialog.Builder b2 = new AlertDialog.Builder(mContext)
                .setTitle("提示").setMessage(message)
                .setPositiveButton("确定",
                        new AlertDialog.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {
                                result.confirm();
                                dialog.dismiss();
                                // MyWebView.this.finish();
                            }
                        });

        b2.setCancelable(true);
        b2.create();
        b2.show();
        return true;
    }


    @Override
    public void onProgressChanged(WebView view, int newProgress) {
        if (newProgress == 100) {
            progressbar.setVisibility(View.GONE);
        } else {
            if (progressbar.getVisibility() == View.GONE)
                progressbar.setVisibility(View.VISIBLE);
            progressbar.setProgress(newProgress);
        }
        super.onProgressChanged(view, newProgress);
    }

    public void bindProgressBar(ProgressBar progressbar) {
        this.progressbar = progressbar;
    }

//    //扩展浏览器上传文件
//    //3.0++版本
//    public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
//        L.e("3.0++版本");
//        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
//        i.addCategory(Intent.CATEGORY_OPENABLE);
//        i.setType("*/*");
//        mContext.startActivityForResult(Intent.createChooser(i, "File Chooser"), SELECT_PHOTO_FOR4);
//    }
//    //3.0--版本
//    public void openFileChooser(ValueCallback<Uri> uploadMsg) {
////        mUploadMessage = uploadMsg;
//        L.e("3.0--版本");
//        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
//        i.addCategory(Intent.CATEGORY_OPENABLE);
//        i.setType("*/*");
//        mContext.startActivityForResult(Intent.createChooser(i, "file Browser"), SELECT_PHOTO_FOR4);
//    }
}
